// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.policies;

import org.dengues.designer.ui.process.commands.CompBlockCreateCommand;
import org.dengues.designer.ui.process.commands.CompBlockSetConstraintCommand;
import org.dengues.designer.ui.process.commands.CompNodeSetConstraintCommand;
import org.dengues.designer.ui.process.commands.CompNoteSetConstraintCommand;
import org.dengues.designer.ui.process.commands.CompNodeCreateCommand;
import org.dengues.designer.ui.process.commands.CompNoteCreateCommand;
import org.dengues.designer.ui.process.models.CompBlock;
import org.dengues.designer.ui.process.models.CompNode;
import org.dengues.designer.ui.process.models.CompNote;
import org.dengues.designer.ui.process.models.CompProcess;
import org.dengues.designer.ui.process.parts.CompBlockPart;
import org.dengues.designer.ui.process.parts.CompNodePart;
import org.dengues.designer.ui.process.parts.CompNotePart;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;

/**
 * qzhang class global comment. Detailled comment <br/>
 */
public class ProcessLayoutPolicy extends XYLayoutEditPolicy {

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editpolicies.ConstrainedLayoutEditPolicy#createChangeConstraintCommand(org.eclipse.gef.EditPart,
     * java.lang.Object)
     */
    @Override
    protected Command createChangeConstraintCommand(EditPart child, Object constraint) {
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editpolicies.ConstrainedLayoutEditPolicy#createChangeConstraintCommand(org.eclipse.gef.requests.ChangeBoundsRequest,
     * org.eclipse.gef.EditPart, java.lang.Object)
     */
    @Override
    protected Command createChangeConstraintCommand(ChangeBoundsRequest request, EditPart child, Object constraint) {
        if (child instanceof CompNodePart && constraint instanceof Rectangle) {
            return new CompNodeSetConstraintCommand(request, (CompNode) child.getModel(), (Rectangle) constraint);
        } else if (child instanceof CompNotePart && constraint instanceof Rectangle) {
            return new CompNoteSetConstraintCommand(request, (CompNote) child.getModel(), (Rectangle) constraint);
        } else if (child instanceof CompBlockPart && constraint instanceof Rectangle) {
            return new CompBlockSetConstraintCommand(request, (CompBlock) child.getModel(), (Rectangle) constraint);
        }
        return super.createChangeConstraintCommand(request, child, constraint);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editpolicies.LayoutEditPolicy#getCreateCommand(org.eclipse.gef.requests.CreateRequest)
     */
    @Override
    protected Command getCreateCommand(CreateRequest request) {
        Rectangle constraint = (Rectangle) getConstraintFor(request);
        Command command = null;
        if (CompNote.class.equals(request.getNewObjectType())) {
            command = new CompNoteCreateCommand((CompProcess) getHost().getModel(), (CompNote) request.getNewObject(),
                    constraint);
        } else if (CompNode.class.equals(request.getNewObjectType())) {
            command = new CompNodeCreateCommand((CompProcess) getHost().getModel(), (CompNode) request.getNewObject(),
                    constraint.getLocation());
        } else if (CompBlock.class.equals(request.getNewObjectType())) {
            command = new CompBlockCreateCommand((CompProcess) getHost().getModel(),
                    (CompBlock) request.getNewObject(), constraint);
        }
        return command;
    }

}
